package com.cupster.func_db_dbflow.db_main.crud;
import android.util.Log;
import com.cupster.func_db_dbflow.db_main.table.User;
import com.cupster.func_db_dbflow.db_main.table.User_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class UserHelper {
    public static boolean insert(String name){
        User user = new User();
        user.name = name ;
        user.age = 27;
        user.account ="18960";
        user.pwd ="123";
        user.money = 5000.0;
        return user.save();
    }
    //增删改查同理字段筛选
    public static void update(String newName,String name ){
        SQLite.update(User.class)
                .set(User_Table.name.eq(newName))
                .where(User_Table.name.eq(name))
                .execute();
    }

    public static boolean  updateOld(String newName,String name ){
        User user = SQLite.select()
                .from(User.class)
                .where(User_Table.name.is(name))
                .querySingle();
        if (user != null){
            user.name = newName;
            return user.update();
        }else {
            return false;
        }
    }

    public static void delete(String name ){
        SQLite.delete(User.class)
                .where(User_Table.name.is(name))
                .execute();
    }

    //单个查询
    public static String selectUser(){
        User user = SQLite.select().from(User.class)
                .where(User_Table.name.is("Cupster")).querySingle();
        return user.name;
    }
    //多个查询
    public static String selectUserList(){
        List<User> userList = SQLite.select().from(User.class).queryList();
        StringBuilder sb = new StringBuilder();
        for (User u : userList){
            sb.append("\n");
            sb.append(u.name);
        }
        return sb.toString();
    }
    //单个 异步查询
    public static void selectUserAsync(){
        SQLite.select().from(User.class)
                .where(User_Table.name.is("Cupster")).async().querySingleResultCallback(new QueryTransaction.QueryResultSingleCallback<User>() {
                    @Override
                    public void onSingleQueryResult(QueryTransaction transaction, @Nullable User user) {
                        if (user != null){
                            Log.i( "onSingleQueryResult: ","query name : "+user.name);
                        }
                    }
                }).error(new Transaction.Error() {
                    @Override
                    public void onError(@NonNull Transaction transaction, @NonNull Throwable error) {
                        Log.i( "onError: ","");
                    }
                }).success(new Transaction.Success() {
                    @Override
                    public void onSuccess(@NonNull Transaction transaction) {
                        Log.i( "onSuccess: ",transaction.name());
                    }
                }).execute();
    }
    //数据库升级
}
